<?php
/**
 * The db class is instantiated by kiwee to carry out all of it's database queries.  
 * Note:  It is not used by the management interface
 * 
 * Date: July 9, 2007
 * Author: Chris Burdick
 * 
 **/

	class db {
		// Constructor grabs the config file and connects to the database
		function __construct(){
			global $modx;
		}
		
		// Select
		public function sel($prfx, $tbl, $fld, $whr=''){
			global $modx;
			if ($whr==""){
				$qry = 'SELECT ' . $fld . ' FROM ' . $prfx . $tbl;
			} else {
				$qry = 'SELECT ' . $fld . ' FROM ' . $prfx . $tbl . ' WHERE ' . $whr;	
			}

			$result = $modx->db->query($qry);
			if ($result == NULL) return NULL;
			$numrows = mysql_num_rows($result);
			for ($i=0; $i < $numrows; $i++){
				$row = mysql_fetch_array($result);
				$temp[$i] = $row;
			}
			
			mysql_free_result($result);
			return $temp;
		}
		
		public function upd($prfx, $tbl, $qry, $whr=''){
			global $modx;
			if ($whr==""){
				$qry = 'UPDATE ' . $prfx . $tbl . ' SET	' . $qry;
			} else {
				$qry = 'UPDATE ' . $prfx . $tbl . ' SET ' . $qry . ' WHERE ' . $whr;	
			}
			$modx->db->query($qry) or die ("MYSQL ERROR: " . mysql_error());
		}

//TODO: Finish this method

/*		function rep($prfx, $tbl, $qry, $whr){
		}
*/		
		public function del($prfx, $tbl, $whr){
			global $modx;
			$qry = 'DELETE FROM ' . $prfx . $tbl . ' WHERE ' . $whr;	
			$modx->db->query($qry) or die ("MYSQL ERROR: " . mysql_error());
		}
		
		public function ins($prfx, $tbl, $flds, $vals){
			global $modx;
			$qry = 'INSERT INTO ' . $prfx . $tbl . ' (' . $flds . ') VALUES (' . $vals . ')';
			$modx->db->query($qry) or die ("MYSQL ERROR: " . mysql_error());
		}
	}

?>